//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//alert(handleResize());
//document.getElementById("accordion").style.height=

var Flag_Search_Panel_Status="Search";

$(document).ready(function() {

    $("#search").click(function() {
            if($("#searchbox").val()!="")
                                    {
				$("#searchresult").html('<img src="/images/loading/loading9.gif">');
                                if($("#accordion").is(":visible")==false){$("#tdShowHide").html('<img src="/images/loading/loading.gif">');Flag_Search_Panel_Status="Search";showAccordion();}
				var cValue = $("#searchbox").val();
				$.post("http://"+location.hostname+"/searchresult/Index", {search: cValue}, function(data) {
                                $("#searchresult").html(data);
                                $("#TotalCount").text(" for: " + $("#searchbox").val());

                                  if($("#accordion").is(":visible")==false)
                                      {
                                       //EnlargeChart(document.getElementById("reportarea"),"1");
                                       Flag_Search_Panel_Status="Search";
                                        showAccordion();
                       }
				});

				return false;
                                    }
                                });
                            });




$(document).ready(function() {

            $("#form1").submit(function() {

                    if($("#searchbox").val()!="")
                            {
				$("#searchresult").html('<img src="/images/loading/loading9.gif">');
                                
                              if($("#accordion").is(":visible")==false){$("#tdShowHide").html('<img src="/images/loading/loading.gif">');Flag_Search_Panel_Status="Search";showAccordion();}

				var cValue = $("#searchbox").val();
				$.post("http://"+location.hostname+"/searchresult/Index", {search: cValue}, function(data) {
                                $("#searchresult").html(data);
                                
                                $("#TotalCount").text(" for: " + $("#searchbox").val());

                              if($("#accordion").is(":visible")==false)
                                  {
                                  
                                    Flag_Search_Panel_Status="Search";
                                    showAccordion();
                                    
                                  }

                                        });

				return false;
                               }
                                            });
                            });


$(document).ready(function(){
        //global vars
	var searchBoxes = $(".text");
	
	var searchBox2 = $("#searchbox");
	var searchBox2Default = "Search the web...";

	//Effects for both searchbox
	searchBoxes.focus(function(e){
		$(this).addClass("active");
	});
	searchBoxes.blur(function(e){
		$(this).removeClass("active");
	});

	//Searchbox1, set focus when document is ready
	

	//Searchbox2 show/hide default text if needed
	searchBox2.focus(function(){
		if($(this).attr("value") == searchBox2Default) $(this).attr("value", "");
	});
	searchBox2.blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", searchBox2Default);
	});
});

$(function() {

		$( "#accordion" ).accordion({
                    collapsible: true
                });
});


$(function() {

		$( "#accordion" ).accordion({

                    change: function(event, ui) { 
                     
                     if((ui.newHeader.text()=="History" && sessionStorage.getItem('history')!=null) ||(ui.newHeader.text()=="History" && sessionStorage.getItem('history_overlay')!=null)||(ui.newHeader.text()=="History" && sessionStorage.getItem('nonspecies')!=null))
                         {
                             Flag_Search_Panel_Status="History";
                            $("#history").html('<img src="/images/loading/loading9.gif">');
//                            alert(sessionStorage.getItem('history'));

                             $.post("http://"+location.hostname+"/searchresult/History", {searchhistory: sessionStorage.getItem('history')+"OLY"+sessionStorage.getItem('history_overlay')+"OLY"+sessionStorage.getItem('nonspecies')}, function(data) {
                                $("#history").html(data);
                                });
                         }
                     if(ui.newHeader.text()=="Citation")
                         {
                             Flag_Search_Panel_Status="Citation";
                         }
                     if(ui.newHeader.text()!="Citation" && ui.newHeader.text()!="History" && ui.newHeader.text()!="")
                         {
                            Flag_Search_Panel_Status="Search";
                            if(document.getElementsByName("selectResult").length>0)
                            {setFilter("2","");}
                         }
                                            }
		});   
	});


$(document).ready(function(){
$('#accordion').hide(); // Hide div by default
$('.btn-slide').click(function() {
showAccordion();
return false;
});
});



function showAccordion()
{
    updateShowHideStatus(document.getElementById('hfBtnshowhideStatus'),'imgshowhide','/images/show_accordion.png','/images/hide_accordion.png');
    $("#tdShowHide").html('');
}

function updateShowHideStatus(btnStatus, imgObservation, enablePic, disablePic)
{

    if(btnStatus)
    {

        var current_div_id="";
        if(Flag_Search_Panel_Status=="Search")
                     {current_div_id="#searchresult";}
                else if(Flag_Search_Panel_Status=="Citation")
                      {current_div_id="#citation";}
                else if(Flag_Search_Panel_Status=="History")
                    {current_div_id="#history";}
                    
                    
        if(btnStatus.value == "1")
        {
            document[imgObservation].src = disablePic;
            btnStatus.value = "0";
            $(current_div_id).hide();
            if($("#reportarea").is(":visible")==true)
                    {
                        $("#accordion").show(500,function(){
                $(current_div_id).show(500);
                                                 document.getElementById("chart_canvas").style.width  = (document.documentElement.clientWidth-800) + "px";
                          if (Flag_Search_Panel_Status=="Search")
                {setFilter("2", "");}
            else if (Flag_Search_Panel_Status=="History")
                {setFilter_History("2", "");}
                    $("#reportarea").animate({"width": (document.documentElement.clientWidth-405)+"px"}, 1000 ,function(){

            });


            });
                    }
                else
                    {document.getElementById("reportarea").style.width  = (document.documentElement.clientWidth)-405 + "px";
                     document.getElementById("chart_canvas").style.width  = (document.documentElement.clientWidth-800) + "px";
            $("#accordion").show(500,function(){
                $(current_div_id).show(500);
               if (Flag_Search_Panel_Status=="Search")
                {setFilter("2", "");}
            else if (Flag_Search_Panel_Status=="History")
                {setFilter_History("2", "");}
                return;
            });
                    }

        }
        else
        {
            document[imgObservation].src = enablePic;
            btnStatus.value = "1";
            $(current_div_id).hide(500,function(){
                $("#accordion").hide(500);
       
        if($("#reportarea").is(":visible")==true)
                {
                $("#reportarea").animate({"width": document.documentElement.clientWidth+"px"}, {duration: 1000});
                $("#chart_canvas").animate({"width": (document.documentElement.clientWidth-400) + "px"},1000,function(){
                        if (Flag_Search_Panel_Status=="Search")
                {setFilter("2", "");}
            else if (Flag_Search_Panel_Status=="History")
                {setFilter_History("2", "");}
                return;
                });
           

                }
       else
            {document.getElementById("reportarea").style.width  = (document.documentElement.clientWidth) + "px";
             document.getElementById("chart_canvas").style.width  = (document.documentElement.clientWidth-400) + "px";
              if (Flag_Search_Panel_Status=="Search")
                {setFilter("2", "");}
            else if (Flag_Search_Panel_Status=="History")
                {setFilter_History("2", "");}
            }


                       

            });
            // Set the Chart width to 100/60% when RightNav Collapse/Expand
            //EnlargeChart(document.getElementById("reportarea"), btnStatus.value);
            //document.getElementById("reportarea").style.width  = (document.documentElement.clientWidth) + "px";
            

            
            
                
        }
    }
}



       $(function() {
                $.post("http://"+location.hostname+"/searchresult/Autocomplete", {}, function(data) {
                $( "#searchbox" ).autocomplete(data.split("###"),{
                minChars: 0,
		max: 10,
		autoFill: false,
		mustMatch: false,
		matchContains: "word",
		scrollHeight: 220
                
//                        minLength: 2,
//                        select: function( event, ui ) {
//

//                        }
		}
		);

                    $("#searchbox").result(function() {


                        if ($("#searchbox").val()!="" && $("#searchbox").val()!="Web Search ...")
                            {
                                //$("#searchbox").val(ui.item.value);

                            	$("#searchresult").html('<img src="/images/loading/loading9.gif">');
                                if($("#accordion").is(":visible")==false){$("#tdShowHide").html('<img src="/images/loading/loading.gif">');Flag_Search_Panel_Status="Search";showAccordion();}
				var cValue = $("#searchbox").val();
                                
				$.post("http://"+location.hostname+"/searchresult/Index", {search: cValue}, function(data) {
                                $("#searchresult").html(data);
                                $("#TotalCount").text(" for: " + $("#searchbox").val());
                                  if($("#accordion").is(":visible")==false)
                                      {
                                       Flag_Search_Panel_Status="Search";
                                        showAccordion();
                                      }
				});
				return false;
                            }

    });


                });
	});


$(document).ready(function() {
    
    document.getElementById("content").style.height  = document.documentElement.clientHeight-70 + "px";
    document.getElementById("reportarea").style.height  = (document.documentElement.clientHeight-70)/3 + "px";
    document.getElementById("chart_canvas").style.height  = (document.documentElement.clientHeight-70)/3 + "px";
    document.getElementById("chart_legend").style.height  = (document.documentElement.clientHeight-280)/3 + "px";
    document.getElementById("accordion").style.height  = (document.documentElement.clientHeight-190) + "px";
    document.getElementById("searchresult").style.height  = (document.documentElement.clientHeight-190) + "px";
    document.getElementById("citation").style.height  = (document.documentElement.clientHeight-190) + "px";
    document.getElementById("history").style.height  = (document.documentElement.clientHeight-190) + "px";
    document.getElementById("reportarea").style.width  = (document.documentElement.clientWidth) + "px";
    document.getElementById("chart_canvas").style.width  = (document.documentElement.clientWidth-200) + "px";
        $(window).resize(function() {
            document.getElementById("reportarea").style.height  = ((document.documentElement.clientHeight-70)/3)+  "px";
            document.getElementById("chart_canvas").style.height  = (document.documentElement.clientHeight-70)/3 + "px";
            document.getElementById("chart_legend").style.height  = (document.documentElement.clientHeight-280)/3 + "px";

            document.getElementById("accordion").style.height  = (document.documentElement.clientHeight-190) + "px";
            document.getElementById("searchresult").style.height  = (document.documentElement.clientHeight-190) + "px";
            document.getElementById("citation").style.height  = (document.documentElement.clientHeight-190) + "px";
            document.getElementById("history").style.height  = (document.documentElement.clientHeight-190) + "px";
            document.getElementById("content").style.height  = document.documentElement.clientHeight-70 + "px";
            if (Flag_Search_Panel_Status=="Search")
                {setFilter("2", "");}
            else if (Flag_Search_Panel_Status=="History")
                {setFilter_History("2", "");}
        
        });
});



